﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Purchase;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.PurchaseStorags
{
    public class MaterialAddHandler : IRequestHandler<MaterialAddCommand, ResultsDTO>
    {
        private IBateRepository<Material> materia;
        private readonly ILogger<MaterialAddHandler> _logger;
        public MaterialAddHandler(IBateRepository<Material> materia, ILogger<MaterialAddHandler> _logger)
        {
            this.materia = materia;   
            this._logger = _logger;
        }
        public async Task<ResultsDTO> Handle(MaterialAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                DapperHelper helper = new DapperHelper();
                ResultsDTO dto = new ResultsDTO() { Code = 0 };
                Material mate = new Material();
                mate.MaterialId = request.MaterialId;
                mate.MaterialNo = request.MaterialNo;
                mate.ProductManageId = request.ProductManageId;
                mate.Num = request.Num;
                mate.Batch = request.Batch;
                mate.StashId = request.StashId;
                mate.MaterialTime = request.MaterialTime;
                mate.Remark = request.Remark;
                mate.PurchaseNo = request.PurchaseNo;
                string conn = helper.MangoMesWrite;
                string sql = "insert into material values(@MaterialId,@MaterialNo,@ProductManageId,@Num,@Batch,@StashId,@PurchaseNo,@MaterialTime,@Remark)";
                dto.Code = await materia.Add(sql, mate, conn);
                _logger.LogInformation("成功");
                return dto;
            }
            catch (Exception ex)
            {
                _logger.LogError("失败" + ex.Message);
                throw;
            }

          
        }

    }
    
}
